/*
 *所有关于InspectionSheet类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*InspectionSheetService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Domain.IRepositories;
using System.Threading.Tasks;
using System;
using VOL.Domain.Dto.QualityManagement;
using VOL.Domain.Infrastructure;

namespace VOL.Domain.Services
{
    public partial class InspectionSheetService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IBaseRepository<InspectionSheet> _inspectionSheetbaseRepository;
        private readonly IInspectionSheetRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public InspectionSheetService(
            IInspectionSheetRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            IBaseRepository<InspectionSheet> inspectionSheetbaseRepository
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            this._inspectionSheetbaseRepository = inspectionSheetbaseRepository;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        /// <summary>
        /// 获取检验单分页列表数据
        /// </summary>
        /// <param name="queryDTO">查询条件</param>
        /// <returns>返回检验单列表数据</returns>
        public async Task<WebResponseContent> GetInspectionSheetPageData(InspectionSheetQueryDTO queryDTO)
        {
            var response = new WebResponseContent();
            var query = _inspectionSheetbaseRepository.GetAll();

            // 应用查询条件
            if (!string.IsNullOrEmpty(queryDTO.InspectionNo))
                query = query.Where(x => x.InspectionSheetCode.Contains(queryDTO.InspectionNo));

            if (!string.IsNullOrEmpty(queryDTO.ProductName))
                query = query.Where(x => x.MaterialName.Contains(queryDTO.ProductName));

            if (!string.IsNullOrEmpty(queryDTO.BatchNo))
                query = query.Where(x => x.BatchNumber.Contains(queryDTO.BatchNo));

            if (!string.IsNullOrEmpty(queryDTO.Inspector))
                query = query.Where(x => x.InspectionPerson.Contains(queryDTO.Inspector));

            if (queryDTO.StartDate.HasValue)
                query = query.Where(x => x.InspectionDate >= queryDTO.StartDate.Value);

            if (queryDTO.EndDate.HasValue)
                query = query.Where(x => x.InspectionDate <= queryDTO.EndDate.Value);

            // 获取总数
            var total = query.Count();

            // 应用分页并返回数据
            var items = query
                .OrderByDescending(x => x.Id)
                .Skip((queryDTO.Page - 1) * queryDTO.PageSize)
                .Take(queryDTO.PageSize)
                .ToList();

            response.Data = new PageGridData
            {
                rows = items,
                total = total,
                page = queryDTO.Page,
                pageSize = queryDTO.PageSize
            };

            return response;
        }

        /// <summary>
        /// 新增检验单
        /// </summary>
        /// <param name="inspectionSheet">检验单实体</param>
        /// <returns>返回新增结果</returns>
        public async Task<WebResponseContent> AddInspectionSheet(InspectionSheet inspectionSheet)
        {
            var response = new WebResponseContent();
            // 保存数据
            await _inspectionSheetbaseRepository.Add(inspectionSheet);
            return response.OK("添加成功");
        }

        /// <summary>
        /// 修改检验单
        /// </summary>
        /// <param name="inspectionSheet">检验单实体</param>
        /// <returns>返回修改结果</returns>
        public async Task<WebResponseContent> UpdateInspectionSheet(InspectionSheet inspectionSheet)
        {
            var response = new WebResponseContent();
            // 保存数据
            await _inspectionSheetbaseRepository.Update(inspectionSheet);
            return response.OK("修改成功");
        }

        /// <summary>
        /// 批量删除检验单
        /// </summary>
        /// <param name="ids">检验单ID集合</param>
        /// <returns>返回删除结果</returns>
        public async Task<WebResponseContent> DeleteInspectionSheet(int[] ids)
        {
            var response = new WebResponseContent();
            if (ids == null || ids.Length == 0)
            {
                return response.Error("请选择要删除的数据");
            }

            // 批量删除数据
            await _inspectionSheetbaseRepository.DeleteAsync(x => ids.Contains(x.Id));
            return response.OK("删除成功");
        }
    }
}
